package com.tjec.project.system.controller;

import java.util.List;
import java.util.Set;

import com.tjec.common.utils.StringUtils;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderMember;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tjec.common.constant.Constants;
import com.tjec.common.utils.ServletUtils;
import com.tjec.framework.security.LoginUser;
import com.tjec.framework.security.service.SysLoginService;
import com.tjec.framework.security.service.SysPermissionService;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.project.system.domain.SysMenu;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.service.ISysMenuService;
import com.tjec.project.system.service.ISysUserService;

import io.swagger.annotations.ApiOperation;

/**
 * 登录验证
 * 
 * @author tjec
 */
@RestController
public class SysLoginController extends BaseController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IBmsLeaderMemberService bmsLeaderMemberService;
    
    @Autowired
    private ISysUserService userService;

    /**
     * 登录方法
     * 
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(String username, String password, String code, String uuid)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(username, password, code, uuid);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    
    /**
     * 第三方获取token
     * 
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    @PostMapping("/getToken")
    public AjaxResult getToken()
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login("admin");
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 手机号登录方法
     *
     * @param phonenumber 手机号
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    @PostMapping("/phonenumberLogin")
    public AjaxResult phonenumberLogin(String phonenumber, String code, String uuid)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.phonenumberLogin(phonenumber, code, uuid);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        //设置项目负责人工作台菜单是否开窗
        boolean isOpenProjectLeader = false;
        BmsLeaderMember bmsLeaderMember = new BmsLeaderMember();
        bmsLeaderMember.setUserId(user.getUserId());
        bmsLeaderMember.setRole("1");
        List<BmsLeaderMember> list = bmsLeaderMemberService.selectBmsLeaderMemberList(bmsLeaderMember);
        if(!CollectionUtils.isEmpty(list)){
            isOpenProjectLeader = true;
        }
        user.setIsOpenProjectLeader(isOpenProjectLeader);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }
    
    /**
     * 修改密码
     */
    @ApiOperation("修改密码")
    @PostMapping("/editPassword")
    public AjaxResult editPassword(String phonenumber, String password, String code, String uuid) {
    	return AjaxResult.success(userService.editUserPwd(phonenumber, password, code, uuid));
    }

    /**
     * 验证码校验
     *
     * @return 验证码校验
     */
    @GetMapping("/codeVerify")
    public AjaxResult codeVerify(String code, String phonenumber, String uuid)
    {
        if(StringUtils.isAnyEmpty(code, phonenumber, uuid)){
            return AjaxResult.error("参数不能为空");
        }
        return toAjax(userService.codeVerify(code, phonenumber, uuid));
    }
}
